//
//  MypromoteVC.m
//  NewPro
//
//  Created by 何荧红 on 2018/12/10.
//  Copyright © 2018 何荧红. All rights reserved.
//

#import "MypromoteVC.h"
#import "MyPromoteCell.h"
#import "MyProfitStore.h"

@interface MypromoteVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;


@end

@implementation MypromoteVC
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = NO;
}
- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    return _dataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCSS];
    [self loadData];
}
- (void)loadData{
    [[[MyProfitStore alloc]init] GetMyPromoteWithID:[UserDefaultsManager sharedInstance].user_id Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            NSArray *arr = res[@"data"][@"items"];
            if (arr.count > 0) {
                self.dataArr = [arr mutableCopy];
            }
        }else{
            [self showMBPError:res[@"msg"]];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } Failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
- (void)initCSS{
    self.view.backgroundColor = [UIColor colorWithHexString:@"F4F4F4"];
    ViewHeader *header = [[ViewHeader alloc]initWithDefaultString:@"合伙人贡献分润"];
    [self.view addSubview:header];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 120 + iPhoneX_Top, SCREEN_WIDTH, SCREEN_HEIGHT - 120 - iPhoneX_Top) style:UITableViewStyleGrouped];
    [self.tableView registerClass:[MyPromoteCell class] forCellReuseIdentifier:@"MyPromoteCellID"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (self.dataArr.count > 0) {
            [self.dataArr removeAllObjects];
        }
        [self loadData];
    }];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(header.mas_bottom);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.dataArr.count > 0) {
        NSDictionary *dic = self.dataArr[section];
        NSArray *arr = dic[@"customs"];
        return arr.count;
    }
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dataArr.count > 0) {
        NSDictionary *dic = self.dataArr[indexPath.section];
        NSArray *arr = dic[@"customs"];
        if (indexPath.row == arr.count - 1) {
            return 44;
        }
    }
    return 49;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 31;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    ViewHeader *header = [[ViewHeader alloc]initWithPromote];
    if (self.dataArr.count > 0) {
        NSDictionary *dic = self.dataArr[section];
        header.PromotetimeStr = dic[@"month"];
    }
    return header;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyPromoteCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyPromoteCellID"];
    if (self.dataArr.count > 0) {
        NSDictionary *dic = self.dataArr[indexPath.section];
        NSArray *arr = dic[@"customs"];
        NSDictionary *subdic = arr[indexPath.row];
        cell.infodic = subdic;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
@end
